/**
 * 仓库
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.repository;


import com.sxf.crm.entity.UserLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface UserLogRepository extends JpaRepository<UserLog, Long> {
    Page<UserLog> findAllByOrderByCreateAtDesc(Pageable pageable);
    
    @Query("SELECT ul FROM UserLog ul WHERE " +
           "(:username IS NULL OR ul.user.username LIKE %:username%) AND " +
           "(:action IS NULL OR ul.action = :action) AND " +
           "(:startDate IS NULL OR ul.createAt >= :startDate) AND " +
           "(:endDate IS NULL OR ul.createAt <= :endDate) " +
           "ORDER BY ul.createAt DESC")
    Page<UserLog> findByConditions(
        @Param("username") String username,
        @Param("action") String action,
        @Param("startDate") Long startDate,
        @Param("endDate") Long endDate,
        Pageable pageable
    );
} 